﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApp.BaseDataSvc;
using System.Text;
using YY.Common;

namespace WebApp
{
    public partial class MainFrm : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(MainFrm), this);
            this.IsPermitFrm = false;
            WebCommon.CheckLogin(this, 0);
            if (Session["User"] != null)
            {
                TB_User user = Session["User"] as TB_User;
                this.spanUserName.InnerHtml = user.userName;
                this.spanRoleName.InnerHtml = Enum.GetName(typeof(EnumCommon.SysRoleType), user.role);
                this.menuDiv.InnerHtml = GetMenuHtml();
            }
        }

        string GetMenuHtml()
        {
            StringBuilder sb = new StringBuilder();
            IEnumerable<TB_SysModule> datas = this.BaseDataProxy.GetUserMenus2((Session["User"] as TB_User).role, null);
            if (datas != null)
            {
                datas.OrderBy(p => p.menuOrder);
                var menuBars = from p in datas group p by p.pMenuName into g select g;
                foreach (var s in menuBars)
                {
                    sb.AppendLine(string.Format("<div title=\"{0}\" data-options=\"iconCls:'icon-ok'\" style=\"padding: 10px;\">", s.Key));
                    var menus = datas.Where(p => p.pMenuName == s.Key);
                    if (menus.Any())
                    {
                        sb.AppendLine("<ul class=\"easyui-tree\">");
                        foreach (TB_SysModule menu in menus)
                        {
                            sb.AppendLine(string.Format("<li><span><a href=\"#\" onclick=\"addTab('{0}','{1}')\">{2}</a></span></li>", menu.moduleName, menu.url, menu.moduleName));
                        }
                        sb.AppendLine("</ul>");
                    }
                    sb.AppendLine("</div>");
                }
            }
            return sb.ToString();
        }
    }
}
